package com.example.springboot.controller;

import com.example.springboot.common.result.Result;
import com.example.springboot.common.request.CategoryListRequest;
import com.example.springboot.entity.Category;
import com.example.springboot.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据分类名称模糊查询
     * @param categoryListRequest
     * @return
     */
    @GetMapping("/page")
    public Result page(CategoryListRequest categoryListRequest) {
        return Result.success(categoryService.page(categoryListRequest));
    }

    /**
     * 添加图书分类
     * @param category
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    public Result save(@RequestBody Category category) throws Exception {
        categoryService.save(category);
        return Result.success();
    }

    /**
     * 根据id获取需要修改的图书分类的信息
     * @param id
     * @return
     */
    @GetMapping("/update/{id}")
    public Result getById(@PathVariable Integer id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 更新图书分类信息
     * @param category
     * @return
     * @throws Exception
     */
    @PutMapping("/update")
    public Result editUser(@RequestBody Category category) throws Exception {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 根据id删除图书分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        categoryService.deleteById(id);
        return Result.success();
    }




}
